/**
 * Created by wangjb925@gmail.com
 * 表单数据验证和数据获取
 */
define('util', function(require, exports, module) {
	var isNull = function(value) {
		if (!value || value.length == 0) {
			return true;
		}
		return false;
	};
	var getRegexByDataType = function(dataType) {
		switch (dataType) {
            case 'Int':
                return /^[1-9]\d*$/;
			case 'Number':
				return /^-?(\d+|[1-9]\d*\.\d+|0\.\d*[1-9]\d*|0?\.0+|0)$/;
			case 'Email':
				return /^\w+([-+.]\w+)*@\w+([-.]\\w+)*\.\w+([-.]\w+)*$/;
			case 'English':
				return /^[A-Za-z]+$/;
			case 'LetterAndNumber':
				return /^[0-9a-zA-Z]+$/;
			case 'Chinese':
				return /^[\u0391-\uFFE5]+$/;
			case 'MobilePhone':
				return /^\d+$/;
			case 'TelePhone':
				return /^\d+$/;
			case 'IdCard':
				return /^\d{15}(\d{2}[A-Za-z0-9])?$/;
			default:
				return null;
		}
	};
	var getDataTypeTip = function(dataType) {
		switch (dataType) {
            case 'Int':
                return '仅支持整数!';
			case 'Number':
				return '仅支持数字!';
			case 'Email':
				return 'Email格式不正确!';
			case 'English':
				return '仅支持英文字符!';
			case 'LetterAndNumber':
				return '仅支持英文和数字!';
			case 'Chinese':
				return '仅支持中文字符!';
			case 'MobilePhone':
				return '手机号码格式不正确!';
			case 'TelePhone':
				return '电话号码格式不正确!';
			case 'IdCard':
				return '身份证号码格式不正确!';
			default:
				return '数据不合法!';
		}
	};
	var showMessage = function($element, type, message) {
		var formGroup = $element.closest('.form-group').attr('class', 'form-group');
		var helpBlock = formGroup.find('.help-block');
		if (helpBlock.length > 0) {
			helpBlock.text(message);
			formGroup.addClass(type);
			if (!$element.is('select') && !$element.parent().hasClass('date')){
				formGroup.find('.fa').attr('class', getSignClass(type));
			}
		} else {
			if (!$element.is('select') && !$element.parent().hasClass('date') && !$element.is('textarea')) {
				$('<i class="' + getSignClass(type) + '"></i>').insertAfter($element).on('click', {
					input : $element,
					type : type,
					formGroup : formGroup
				}, function(e) {
					if (type == 'has-error' || type == 'has-warning') {
						e.data.input.val('');
						e.data.formGroup.removeClass(e.data.type);
					}
				});
			}
			formGroup.append($('<div class="help-block col-xs-12 col-sm-reset inline"></div>').text(message)).addClass(type);
		}
	};
	var initValidate = function(form) {
		form.find('input,select,textarea').each(function() {
			var $element = $(this);
			var maxLength = $element.attr('maxLength');
			if (maxLength) {
				$element.on('keydown', {
					maxLength : maxLength
				}, function() {
					var $this = $(this);
					var value = $this.val().replace(/(^\s*)|(\s*$)/g, "");
					if (value.length == parseInt(maxLength)) {
						showMessage($this, 'has-info', '该选项最长' + maxLength + '位!');
						return;
					}
				});
			}
			$element.on('blur', function() {
				var $this = $(this);
				var value = $(this).val().replace(/(^\s*)|(\s*$)/g, "");
                var required = $this.attr('required');
				if (required && isNull(value)) {
					showMessage($element, 'has-error', '该选项不能为空!');
					return;
				}
				var minLength = $this.attr('minLength');
				if (minLength && value.length < parseInt(minLength)) {
					showMessage($element, 'has-error', '该选项最少' + minLength + '位!');
					return;
				}
				var dataType = $this.attr('data-type');
				if ((required || minLength) && dataType && !getRegexByDataType(dataType).test(value)) {
					showMessage($element, 'has-error', getDataTypeTip(dataType));
					return;
				}
				var regex = $this.attr('regex');
				if ((required || minLength) && regex && !new RegExp(regex).test(value)) {
					showMessage($element, 'has-error', '数据不合法!');
					return;
				}
				showMessage($element, 'has-success', '');
			});
		});
	};
	var validate = function(form) {
		if (form.find('.has-error, .has-warning').length > 0) {
			return false;
		}
		var elements = form.find('input,select');
		for (var i = 0, j = elements.length; i < j; i++) {
			var $element = $(elements[i]);
			var value = $element.val().replace(/(^\s*)|(\s*$)/g, "");
            var required = $element.attr('required');
			if (required && isNull(value)) {
				showMessage($element, 'has-error', '该选项不能为空!');
				return false;
			}
			var minLength = $element.attr('minLength');
			if (minLength && value.length < parseInt(minLength)) {
				showMessage($element, 'has-error', '该选项最少' + minLength + '位!');
				return false;
			}
			var dataType = $element.attr('data-type');
			if ((required || minLength) && dataType && !getRegexByDataType(dataType).test(value)) {
				showMessage($element, 'has-error', getDataTypeTip(dataType));
				return false;
			}
			var regex = $element.attr('regex');
			if ((required || minLength) && regex && !regex.test(value)) {
				showMessage($element, 'has-error', '数据不合法!');
				return false;
			}
		}
		return true;
	};
	var getFormData = function(form) {
		var data = {};
		form.find('input,select,textarea').each(function() {
			var $element = $(this);
			var type = $element.attr('type');
			var id = $element.attr('id');
			var value = $element.val().replace(/(^\s*)|(\s*$)/g, "").replace(/</g, '&lt;').replace(/>/g, '&gt;');
			if (type == 'checkbox' || type == 'radio') {
				if($element.is(':checked')){
					data[id] = value;
				}
			} else {
				data[id] = value;
			};
		});
		return data;
	}
	var getSignClass = function(type) {
		switch (type) {
			case 'has-info':
				return 'fa fa-info-circle';
			case 'has-success':
				return 'fa fa-check-circle';
			case 'has-warning':
				return 'fa fa-exclamation-triangle';
			default:
				return 'fa fa-times-circle';
		}
	};
	module.exports = {
		initValidate : initValidate,
		getFormData : getFormData,
		validate : validate,
		showMessage: showMessage
	};
});
